class Solution
{
    vector<vector<int>> ret;
    vector<int> path;
    vector<int> check;
public:
    vector<vector<int>> permute(vector<int>& nums)
    {
        int n = nums.size();
        check = vector<int>(n);
        dfs(nums);
        return ret;
    }
    void dfs(vector<int>& nums)
    {
        if (path.size() == nums.size())
        {
            ret.push_back(path);
            return;
        }
        for (int i = 0; i < nums.size(); i++)
        {
            if (check[i] == false)
            {
                path.push_back(nums[i]);
                check[i] = true;
                dfs(nums);
                path.pop_back();
                check[i] = false;
            }
        }
    }
};

class Solution
{
    vector<vector<int>> ret;
    vector<int> path;
public:
    vector<vector<int>> subsets(vector<int>& nums)
    {
        dfs(nums, 0);
        return ret;
    }
    void dfs(vector<int>& nums, int pos)
    {
        if (pos == nums.size())
        {
            ret.push_back(path);
            return;
        }
        path.push_back(nums[pos]);
        dfs(nums, pos + 1);
        path.pop_back();
        dfs(nums, pos + 1);
    }
};

class Solution
{
    vector<string> ret;
    string path;
    vector<string> hash = { "", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz" };
public:
    vector<string> letterCombinations(string digits)
    {
        if (digits.size() == 0) return {};
        dfs(digits, 0);
        return ret;
    }
    void dfs(string digits, int pos)
    {
        if (pos == digits.size())
        {
            ret.push_back(path);
            return;
        }
        for (auto ch : hash[digits[pos] - '0'])
        {
            path.push_back(ch);
            dfs(digits, pos + 1);
            path.pop_back();
        }

    }
};
class Solution
{
    int sum = 0;
    vector<vector<int>> ret;
    vector<int> path;
    int target;
public:
    vector<vector<int>> combinationSum(vector<int>& candidates, int _target)
    {
        target = _target;
        dfs(candidates, 0);
        return ret;
    }
    void dfs(vector<int>& candidates, int pos)
    {
        if (sum >= target)
        {
            if (sum == target) ret.push_back(path);
            return;
        }
        for (int i = pos; i < candidates.size(); i++)
        {
            sum += candidates[i];
            path.push_back(candidates[i]);
            dfs(candidates, i);
            sum -= candidates[i];
            path.pop_back();
        }

    }
};